package org.loofer.ext.component.logger;

import android.content.Context;
import android.util.Log;
import android.util.Pair;
import org.loofer.ext.utils.ArrayUtils;
import org.loofer.ext.utils.JsonUtils;
import org.loofer.ext.utils.ObjectUtils;
import java.util.Collection;
import java.util.Map;

public class Logger {
    private static ILog mLog = ReleaseLogger.getInstance();

    public static void init(Context context, ILog logImpl) {
        if (logImpl == null) {
            throw new NullPointerException("log implementation == null");
        }
        LoggerFile.configure(context);
        mLog = logImpl;
    }

    public static void v(String tag, String text) {
        mLog.v(tag, text);
    }

    public static void v(String tag, Object object) {
        mLog.v(tag, wrapCollection(object));
    }

    public static void v(String tag, String text, Throwable tr) {
        mLog.v(tag, text + '\n' + getStackTraceString(tr));
    }

    public static void d(String tag, String text) {
        mLog.d(tag, text);
    }

    public static void d(String tag, Object object) {
        mLog.d(tag, wrapCollection(object));
    }

    public static void d(String tag, String text, Throwable tr) {
        mLog.d(tag, text + '\n' + getStackTraceString(tr));
    }

    public static void d(String tag, String format, Object... params) {
        d(tag, String.format(format, params));
    }

    public static void i(String tag, String text) {
        mLog.i(tag, text);
    }

    public static void i(String tag, Object object) {
        mLog.i(tag, wrapCollection(object));
    }

    public static void i(String tag, String text, Throwable tr) {
        mLog.i(tag, text + '\n' + getStackTraceString(tr));
    }

    public static void w(String tag, String text) {
        mLog.w(tag, text);
    }

    public static void w(String tag, Throwable tr) {
        mLog.w(tag, getStackTraceString(tr));
    }

    public static void w(String tag, Object object) {
        mLog.w(tag, wrapCollection(object));
    }

    public static void w(String tag, String text, Throwable tr) {
        mLog.w(tag, text + '\n' + getStackTraceString(tr));
    }

    public static void e(String tag, String text, Throwable tr) {
        mLog.e(tag, text + '\n' + getStackTraceString(tr));
    }

    public static void e(String tag, Object object) {
        mLog.e(tag, wrapCollection(object));
    }

    public static void e(String tag, String text) {
        mLog.e(tag, text);
    }

    public static void e(Exception e) {
        mLog.e(e);
    }

    public static void v(String text) {
        mLog.v(text);
    }

    public static void d(String text) {
        mLog.d(text);
    }

    public static void i(String text) {
        mLog.i(text);
    }

    public static void w(String text) {
        mLog.w(text);
    }

    public static void e(String text) {
        mLog.e(text);
    }

    public static String getStackTraceString(Throwable tr) {
        return Log.getStackTraceString(tr);
    }

    public static String wrapCollection(Object object) {
        if (object == null) {
            return ObjectUtils.objectToString(object);
        }
        String simpleName = object.getClass().getSimpleName();
        if (object instanceof Throwable) {
            return getStackTraceString((Throwable) object);
        }
        if (object instanceof String) {
            return (String) object;
        }
        String msg;
        if (object.getClass().isArray()) {
            msg = "over two-dimension array not supported yet";
            switch (ArrayUtils.getArrayDimension(object)) {
                case 1:
                    Pair pair = ArrayUtils.arrayToString(object);
                    msg = simpleName.replace(JsonUtils.EMPTY_JSON_ARRAY, "[" + pair.first + "] {\n") + pair.second + "\n";
                    break;
                case 2:
                    Pair pair1 = ArrayUtils.arrayToObject(object);
                    Pair pair2 = pair1.first;
                    msg = simpleName.replace("[][]", "[" + pair2.first + "][" + pair2.second + "] {\n") + pair1.second + "\n";
                    break;
            }
            return msg + "}";
        } else if (object instanceof Collection) {
            Collection collection = (Collection) object;
            msg = String.format("%s size = %d [\n", new Object[]{simpleName, Integer.valueOf(collection.size())});
            if (!collection.isEmpty()) {
                int flag = 0;
                for (Object item : collection) {
                    String itemString = "[%d]:%s%s";
                    StringBuilder append = new StringBuilder().append(msg);
                    Integer[] numArr = new Object[3];
                    numArr[0] = Integer.valueOf(flag);
                    numArr[1] = ObjectUtils.objectToString(item);
                    int flag2 = flag + 1;
                    numArr[2] = flag < collection.size() + -1 ? ",\n" : "\n";
                    msg = append.append(String.format(itemString, numArr)).toString();
                    flag = flag2;
                }
            }
            return msg + "\n]";
        } else if (!(object instanceof Map)) {
            return ObjectUtils.objectToString(object);
        } else {
            msg = simpleName + " {\n";
            Map<Object, Object> map = (Map) object;
            for (Object key : map.keySet()) {
                Object value = map.get(key);
                msg = msg + String.format("[%s -> %s]\n", new Object[]{ObjectUtils.objectToString(key), ObjectUtils.objectToString(value)});
            }
            return msg + "}";
        }
    }
}
